package com.codingfly.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

@Configuration
public class DatabaseConfig {

    @Primary
    @Bean("orderDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.order")
    public DataSource orderDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean("kucunDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.kucun")
    public DataSource kucunDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean("fundDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.fund")
    public DataSource fundDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Primary
    @Bean("orderJdbcTemplate")
    public JdbcTemplate orderJdbcTemplate(@Qualifier("orderDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    @Primary
    @Bean("orderNamedJdbcTemplate")
    public NamedParameterJdbcTemplate orderNamedJdbcTemplate(@Qualifier("orderJdbcTemplate") JdbcTemplate jdbcTemplate) {
        return new NamedParameterJdbcTemplate(jdbcTemplate);
    }

    @Bean("kucunJdbcTemplate")
    public JdbcTemplate kucunJdbcTemplate(@Qualifier("kucunDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    @Bean("kucunNamedJdbcTemplate")
    public NamedParameterJdbcTemplate kucunNamedJdbcTemplate(@Qualifier("kucunJdbcTemplate") JdbcTemplate jdbcTemplate) {
        return new NamedParameterJdbcTemplate(jdbcTemplate);
    }

    @Bean("fundJdbcTemplate")
    public JdbcTemplate fundJdbcTemplate(@Qualifier("fundDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    @Bean("fundNamedJdbcTemplate")
    public NamedParameterJdbcTemplate fundNamedJdbcTemplate(@Qualifier("fundJdbcTemplate") JdbcTemplate jdbcTemplate) {
        return new NamedParameterJdbcTemplate(jdbcTemplate);
    }

    @Primary
    @Bean(name = "orderTransactionManager")
    public PlatformTransactionManager orderTransactionManager(@Qualifier("orderDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "kucunTransactionManager")
    public PlatformTransactionManager kucunTransactionManager(@Qualifier("kucunDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "fundTransactionManager")
    public PlatformTransactionManager fundTransactionManager(@Qualifier("fundDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

}